package arithmetic.leetCode.yi;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-07-16
 * Time: 16:39
 */
public class SellesV1 {

    public static void main(String[] args) {
        System.out.println(maxProfitV2(new int[]{1, 2, 3, 4, 5}));
    }

    public static int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) return 0;
        int buyMin = prices[0];
        int sellMax = 0;
        for (int i = 0; i < prices.length; i++) {
            buyMin = (buyMin < prices[i]) ? buyMin : prices[i];
            sellMax = (sellMax > prices[i] - buyMin) ? sellMax : prices[i] - buyMin;
        }
        return sellMax;
    }

    public static int maxProfitV2(int[] prices) {
        int max = 0;
        int size = prices.length;
        for (int i = 0; i < size - 1; i++) {
            if (prices[i] < prices[i + 1]) {
                max += prices[i + 1] - prices[i];
            }
        }

        return max;
    }
}
